/*									             Can I Stay a BIT Longer?
								         The Effect of BITs on Political Survival
												   Soumyajit Mazumder
										        (Harvard University)
						
											   	    August 2015
										           Main Results
												   Step 1. Run RIO_BIT_Merge.do
												   Step 2. Run this file
												   Step 3. Run Matched_Prep.do
												   Step 4. Run match.R
												   Step 5. Run MatchMerge.do
												   Step 6. Run Matched_Analysis.do
												   Step 7. Run Causal_Mechanism.do
												   
*/

cd "INSERT DIRECTORY WHERE REPLICATION PACKAGE IS LOCATED"

use "Replication.dta", clear
*gen float endobserv = date(endobs, "MDY")
format endobserv %td

stset endobserv, id(leadid) fail(fail==1) origin(eindate) enter(time mdy(1,1,1965)) scale(365.25)
eststo clear

*******************************************************
*   The following set of code generates a             *
*   number of key explanatory variables               *
*******************************************************

//generate logNSBIT
gen logNSBIT = log(NSBITs+1)

//generate logSSBIT
gen logSSBIT = log(SSBITs+1)

//generate interaction b/w polity scores and logNSBIT
gen logNSBITDem = logNSBIT*polity

//generate interaction b/w polity scores and logSSBIT
gen logSSBITDem = logSSBIT*polity

//generate log real gdp per cap
gen logrealgdppc = log(rgdpo/pop)

//generate log population
gen logpop = log(pop)

//generate interaction of fdi and polity scores
gen fdidem = fdi*polity

//generate measure of BITs inherited from predecessors
gen inherited_BITs = log(1+totalBITs-(NSBITs+SSBITs))

//generate log pta signed variable and interaction from Hollyer and Rosendorff paper
gen log_num_pta_sign=.
replace log_num_pta_sign=ln(num_pta_lead_sign+1)

gen pta_sign_dem = pta_lead_sign*polity
gen pta_num_dem = log_num_pta_sign*polity

//generate BIT "treatment" variable for matching procedures
gen BIT_lead_sign=0
replace BIT_lead_sign=1 if NSBITs>0

gen total_bits = NSBITs+SSBITs
gen logTotBITs = log(total_bits+1)

gen TotBITsDem = logTotBITs*polity

label var logNSBIT "North-South BITs (Logged)"
label var logSSBIT "South-South BITs (Logged)"
label var logSSBITDem "South-South BITs*Polity"
label var polity "Polity II"
label var logNSBITDem "North-South BITs*Polity"
label var log_num_pta_sign "PTAs Signed (Logged)"
label var inherited_BITs "Num. BITs Inherited"
label var logrealgdppc "GDP per Capita (2005 PPP, Logged)"
label var logpop "Population (Logged)"
label var growth "GDP Growth Rate"
label var fdi "FDI (\% of GDP)"
label var fuelexports "Fuel Exports (\% of Merchandise Exports)"
label var aid "Aid (\% of GDP)"
label var fraction_reported "HRV Transparency Index"
label var under_imf "Under IMF Program"
label var wb_all "Num. World Bank Programs"
label var polconiii "Political Constraints"

//summary stats
quietly stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth if OECD~=1, cluster(ccode) nohr

quietly sutex logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth if e(sample), file(summary.tex) replace minmax digits(2) labels title(Summary Statistics) key(summary)

quietly corrtex logNSBIT polity logNSBITDem logSSBIT logSSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all polconiii if e(sample), file("corr.tex") replace digits(2) title(Correlation Matrix) key(corr)

**********************************************************************************************************************************************

*****************************************************
*     Cox Proportional Hazards Analysis of          *
*     North-South BITs in force on incumbent        *
*     survival.                                     *
*****************************************************
eststo clear

//Baseline Model
quietly eststo base: stcox logNSBIT polity logNSBITDem logSSBIT logSSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth if OECD~=1, /// 
shared(ccode) forceshared nohr

//Free Resources Model
quietly eststo free: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid if OECD~=1, /// 
shared(ccode) forceshared nohr

//Political Risk Model
quietly eststo risk: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported if OECD~=1, /// 
shared(ccode) forceshared nohr

//IFI Model
quietly eststo ifi: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all if OECD~=1, /// 
shared(ccode) forceshared nohr

//Ratification Model
quietly eststo rat: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all polconiii if OECD~=1, /// 
shared(ccode) forceshared nohr

esttab base free risk ifi rat using "Table_1.tex", replace tex se label title(A Model of Political Survival: Cox Proportional Hazards Model\label{table:main}) ///
mtitles("Model 1: Baseline" "Model 2: Free Resources" "Model 3: Political Risk" "Model 4: IFI Involvement" "Model 5: Ratification")
*****************************************************
*     Cox Non-Proportional Hazards Analysis of      *
*     North-South BITs in force on incumbent        *
*     survival.                                     *
*****************************************************

//testing proportional hazards assumption
quietly stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all polconiii if OECD~=1, /// 
cluster(ccode) nohr

quietly estat phtest, detail
quietly estat phtest, log detail

quietly eststo time: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all polconiii if OECD~=1, tvc(logNSBIT polity logNSBITDem log_num_pta_sign) nohr

quietly eststo logtime: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all polconiii if OECD~=1, texp(log(_t)) tvc(logNSBIT polity logNSBITDem log_num_pta_sign) nohr

esttab time logtime using "Table_2.tex", replace tex se label title(Non-Proportional Hazards Model\label{table:nonprop}) ///
mtitles("Model 1: Time" "Model 2: Log(Time)")

*****************************************************
*     Code to generate Figure 1 in the text.        *
*****************************************************

//run model 5 from table 2
quietly stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid under_imf wb_all fraction_reported polconiii if OECD~=1, shared(ccode) forceshared nohr

//creating graphs for North-South BITs in force
sum logNSBIT if e(sample), de

*sets all the variables to appropriate values
local min_log_NSBIT=r(min)
local max_log_NSBIT=r(max)
local mean_log_NSBIT=r(mean)
local p75_log_NSBIT=r(p75)
local min_NSBIT_min_polity=`min_log_NSBIT'*-10
local min_NSBIT_max_polity=`min_log_NSBIT'*10
local mean_NSBIT_min_polity=`mean_log_NSBIT'*-10
local mean_NSBIT_max_polity=`mean_log_NSBIT'*10
local p75_NSBIT_min_polity=`p75_log_NSBIT'*-10
local p75_NSBIT_max_polity=`p75_log_NSBIT'*10
local max_NSBIT_min_polity=`max_log_NSBIT'*-10
local max_NSBIT_max_polity=`max_log_NSBIT'*10

stcurve, hazard at1(polity=-10  logNSBIT = `min_log_NSBIT'  logNSBITDem = `min_NSBIT_min_polity') ///
at2(polity=-10 logNSBIT = `mean_log_NSBIT' logNSBITDem = `mean_NSBIT_min_polity') ///
at3(polity=-10 logNSBIT = `p75_log_NSBIT' logNSBITDem = `p75_NSBIT_min_polity') /// 
at4(polity=-10 logNSBIT = `max_log_NSBIT' logNSBITDem = `max_NSBIT_min_polity') ///
graphregion(fcolor(white)) ytitle(Hazard Rate) xtitle(Years in Office) scheme(s2gcolor) ///
legend(label(1 No BITs) label(2 Mean BITs) label(3 75th Percentile BITs) label(4 Max BITs)) name(minpolity, replace) ///
subtitle(Minimum Polity Score)

stcurve, hazard at1(polity=10  logNSBIT = `min_log_NSBIT'  logNSBITDem =`min_NSBIT_max_polity') ///
at2(polity=10 logNSBIT = `mean_log_NSBIT' logNSBITDem = `mean_NSBIT_max_polity') ///
at3(polity=10 logNSBIT = `p75_log_NSBIT' logNSBITDem = `p75_NSBIT_max_polity') ///
at4(polity=10 logNSBIT =`max_log_NSBIT' logNSBITDem = `max_NSBIT_max_polity') ///
graphregion(fcolor(white)) ytitle(Hazard Rate) xtitle(Years in Office) scheme(s2gcolor) ///
legend(label(1 No BITs) label(2 Mean BITs) label(3 75th Percentile BITs) label(4 Max BITs)) name(maxpolity, replace) ///
subtitle(Maximum Polity Score)

graph combine minpolity maxpolity, ycommon graphregion(fcolor(white)) ///
title(Estimated Hazard Rates for North-South BITs in Force)

graph export "ConditionalEffectUnmatched.eps", replace

******************************ROBUSTNESS CHECKS********************************
eststo clear

//results robust to inclusion of South-South BITs
quietly eststo ss_base: stcox logNSBIT polity logNSBITDem logSSBIT logSSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth if OECD~=1, /// 
cluster(ccode) nohr

quietly eststo ss_free: stcox logNSBIT polity logNSBITDem logSSBIT logSSBITDem  log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid if OECD~=1, /// 
cluster(ccode) nohr

quietly eststo ss_risk: stcox logNSBIT polity logNSBITDem logSSBIT logSSBITDem  log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported if OECD~=1, /// 
cluster(ccode) nohr

quietly eststo ss_ifi: stcox logNSBIT polity logNSBITDem logSSBIT logSSBITDem  log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all if OECD~=1, /// 
cluster(ccode) nohr

quietly eststo ss_rat: stcox logNSBIT polity logNSBITDem logSSBIT logSSBITDem  log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all polconiii if OECD~=1, /// 
cluster(ccode) nohr

esttab ss_base ss_free ss_risk ss_ifi ss_rat using "robust1.tex", replace tex se label title(Robustness Checks\label{table:robust1}) ///
mtitles("Model 1: Baseline" "Model 2: Free Resources" "Model 3: Political Risk" "Model 4: IFI Involvement" "Model 5: Ratification")

//Controlling for country heterogeneity by stratifying regression by country
quietly eststo ctry: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid under_imf wb_all fraction_reported polconiii if OECD~=1, strata(ccode) nohr

//trimming sample of highly durable leaders by 95th, 90th, and 75th percentiles
sum durable if e(sample), de
local p95_durable = r(p95)
local p90_durable = r(p90)
local p75_durable = r(p75)

quietly eststo d95: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid under_imf wb_all fraction_reported polconiii if OECD~=1 & durable<`p95_durable', cluster(ccode) nohr
quietly eststo d90: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid under_imf wb_all fraction_reported polconiii if OECD~=1 & durable<`p90_durable', cluster(ccode) nohr
quietly eststo d75: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid under_imf wb_all fraction_reported polconiii if OECD~=1 & durable<`p75_durable', cluster(ccode) nohr

esttab ctry d95 d90 d75 using "robust2.tex", replace tex se label title(Robustness Checks\label{table:robust2}) ///
mtitles("Model 1: Stratified" "Model 2: Trim 95th Percentile" "Model 3: Trim 90th Percentile" "Model 4: Trim 75th Percentile")

//varying definition of developing countries
gen gdppc=rgdpo/pop

*trim high income countries as per the World Bank's definition
quietly eststo high: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all polconiii if gdppc<13000, cluster(ccode) nohr

*trim upper middle income countries as per the World Bank's definition
quietly eststo upper: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all polconiii if gdppc<4000, cluster(ccode) nohr

//limiting sample to 1980s and 90s where BIT signing is more exogenous as per Jandhyala et al (2011)
quietly eststo norm: stcox logNSBIT polity logNSBITDem log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid fraction_reported under_imf wb_all polconiii if OECD~=1 & year>=1980 & year<2000, cluster(ccode) nohr

//use BDM et al (2003)'s measure of W instead of polity score
gen W = 0
replace W = W+1 if xrcomp>=2
replace W = W+1 if xropen>2
replace W = W+1 if parcomp==5
replace W = W-1 if xrreg==1|xrreg==2
replace W = W/4

gen BITsW = logNSBIT*W

label var W "Winning Coalition Size (W)"
label var BITsW "North-South BITs*W"

quietly eststo w: stcox logNSBIT W BITsW log_num_pta_sign inherited_BITs logrealgdppc logpop growth fdi fuelexports aid under_imf wb_all fraction_reported polconiii if OECD~=1, cluster(ccode) nohr

esttab high upper norm w using "robust3.tex", replace tex se label title(Robustness Checks\label{table:robust3}) ///
mtitles("Model 1: Trim High Income" "Model 2: Trim Upper Middle Income" "Model 3: 1980-1990s" "Model 4: Winning Coalition")

**********************************************************************************************************************************************
